package com.anosine.gpsl.android.ui.activity;

import com.anosine.gpsl.android.GPSLApp;
import com.anosine.gpsl.android.R;
import com.anosine.gpsl.android.base.BaseActivity;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.presenter.LoginPresenter;
import com.anosine.gpsl.android.ui.LoginView;
import com.anosine.gpsl.android.util.ActivityFrgManager;
import com.anosine.gpsl.android.util.DeviceUtil;
import com.anosine.gpsl.android.util.ToastHelper;
import com.anosine.gpsl.android.util.UserInfoProxy;
import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

/**
 * @Title: WelcomeActivity
 * @Package com.anosine.gpsl.android.ui.activity
 * @Description: 欢迎界面
 * @author: sunny
 * @date: 2015-11-17
 * @version V1.0
 */
public class WelcomeActivity extends BaseActivity implements LoginView {
	/**
	 * @Title: onBaseCreate(Bundle savedInstanceState)
	 * @Description: activity初始化
	 * @param: savedInstanceState Activity结束前，如果需要保存状态，
	 *         就在onsaveInstanceState中，将状态数据以key-value的形式放入到savedInstanceState中。
	 *         这样，当一个Activity被创建时，就能从onCreate的参数savedInsanceState中获得状态数据
	 * @return: 无
	 * @throws
	 */
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		// setContentView(R.layout.activity_welcome);
		// new Handler().postDelayed(new Runnable() {
		// /**
		// * @Title: run()
		// * @Description: run()方法是Runnable接口的抽象方法，这个方法就是线程的入口
		// * @param: 无
		// * @return: 无
		// * @throws
		// */
		// public void run() {
		// /* Create an Intent that will start the Main WordPress Activity. */
		// Intent mainIntent = new Intent(WelcomeActivity.this,
		// LoginActivity.class);
		// startActivity(mainIntent);
		// WelcomeActivity.this.finish();
		// }
		// }, 2900); // 2900 for release
		final View view = View.inflate(this, R.layout.activity_welcome, null);
		setContentView(view);
		// MobclickAgent.setDebugMode(false);
		// MobclickAgent.openActivityDurationTrack(false);
		// MobclickAgent.updateOnlineConfig(this);
		// //调用百度推送
		// Resources resource = this.getResources();
		// String pkgName = this.getPackageName();
		// PushManager
		// .startWork(getApplicationContext(),
		// PushConstants.LOGIN_TYPE_API_KEY,
		// getMetaValue(this, "api_key"));
		// CustomPushNotificationBuilder cBuilder = new
		// CustomPushNotificationBuilder(
		// resource.getIdentifier("notification_custom_builder", "layout",
		// pkgName), resource.getIdentifier("notification_icon",
		// "id", pkgName), resource.getIdentifier(
		// "notification_title", "id", pkgName),
		// resource.getIdentifier("notification_text", "id", pkgName));
		// cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
		// cBuilder.setNotificationDefaults(Notification.DEFAULT_VIBRATE);
		// cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);
		// cBuilder.setLayoutDrawable(resource.getIdentifier("gpsl_icon",
		// "drawable", pkgName));
		// cBuilder.setNotificationSound(Uri.withAppendedPath(
		// MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6").toString());
		// // 推送高级设置，通知栏样式设置为下面的ID
		// PushManager.setNotificationBuilder(this, 1, cBuilder);
		// 创建一个AlphaAnimation对象（参数表示从完全不透明到完全透明 1,0）
		AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
		aa.setDuration(2000);// 动画的持续时间
		view.startAnimation(aa);
		// AnimationListener Animation设置监听器
		aa.setAnimationListener(new Animation.AnimationListener() {
			// 持续时间结束后
			@Override
			public void onAnimationEnd(Animation arg0) {
				redirectTo();
			}

			// 重复
			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}
		});

	}

	// 获取ApiKey
//	public static String getMetaValue(Context context, String metaKey) {
//		Bundle metaData = null;
//		String apiKey = null;
//		if (context == null || metaKey == null) {
//			return null;
//		}
//		try {
//			ApplicationInfo ai = context.getPackageManager()
//					.getApplicationInfo(context.getPackageName(),
//							PackageManager.GET_META_DATA);
//			if (null != ai) {
//				metaData = ai.metaData;
//			}
//			if (null != metaData) {
//				apiKey = metaData.getString(metaKey);
//			}
//		} catch (PackageManager.NameNotFoundException e) {
//
//		}
//		return apiKey;
//	}

	@Override
	protected void initViews() {

	}

	private void redirectTo() {
		// 自动登录功能
		if (UserInfoProxy.getInstance().isLoginSuccess()) {

			initActivityOfLogin();

		} else {
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
		}
		finish();
	}

	private void initActivityOfLogin() {
		// 将合成的URL传给服务器
		LoginPresenter presenter = new LoginPresenter(this);

		try {
			presenter.loadLogin(UserInfoProxy.getInstance().getLoginInfo()
					.getUserName(), UserInfoProxy.getInstance().getLoginInfo()
					.getPassWord(), DeviceUtil.getDeviceImei(),
					GPSLApp.getchannelId(), DeviceUtil.getDeviceImsi(),
					Build.BRAND, "1", GPSLApp.getuserId(), Build.MODEL,
					DeviceUtil.getVersionName(this));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setLogin(Login t) {
		if (t != null && "msg_login_suucess".equals(t.message)) {
			ActivityFrgManager.getInstance().finishActivities();
			t.isLoginAuto = true;
			t.setUserName(UserInfoProxy.getInstance().getLoginInfo()
					.getUserName());
			t.setPassWord(UserInfoProxy.getInstance().getLoginInfo()
					.getPassWord());
			t.SaveOid = UserInfoProxy.getInstance().getLoginInfo().SaveOid;
			UserInfoProxy.getInstance().setLoginUserInfo(t);
			startActivity(new Intent(getApplication(), GPSLMainActivity.class));
			finish();
		} else {
			if (t != null && "msg_IMEI_flase".equals(t.message)) {
				Intent intent = new Intent();
				// 设置传递方向
				intent.setClass(getApplication(),
						ValidateOrForgetPwdActivity.class);
				// 绑定数据
				intent.putExtra("status", "validate");
				startActivity(intent);
				finish();

			} else {
				if ("msg_username_flase".equals(t.message)) {
					ToastHelper.toast("你输入的用户名错误!");
					Intent intent = new Intent(getApplication(),
							LoginActivity.class);
					startActivity(intent);
					finish();
				} else if ("msg_password_flase".equals(t.message)) {
					ToastHelper.toast("你输入的密码错误!");
					Intent intent = new Intent(getApplication(),
							LoginActivity.class);
					startActivity(intent);
					finish();
				} else if ("msg_fail".equals(t.message)) {
					ToastHelper.toast("你输入的参数错误!");
				} else if ("msg_login_flase".equals(t.message)) {
					ToastHelper.toast("登录失败!");
					Intent intent = new Intent(getApplication(),
							LoginActivity.class);
					startActivity(intent);
					finish();
				}

			}
		}
	}

}
